// pages/my/address/adressList/adressList.js
import Config from "../../common/config.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addList: {},
    indexAdree: "",
    id: "",
    orderid: "",
    noData: true,
    type: "",
    zf:"",
    bookId:'',
    amount:'',
    a:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(11111,options);
    this.setData({
      type: options.type,
      id: options.dingdanid,
      yy:options.yy,
      zf: options.zf,
      bookId: options.bookId,
      amount: options.amount
    })
    wx.setStorage({
      key: 'zf',
      data: this.data.zf,
    })
    var sessionId = wx.getStorageSync('sessionId')
    var that = this
    // 获取id缓存
    wx.getStorage({
      key: 'id',
      success: function(res) {
        console.log(res.data)
        that.setData({
          id: res.data
        })
      },
    });
    wx.getStorage({
      key: 'orderId',
      success: function(res) {
        console.log(res.data);
        that.setData({
          orderid: res.data
        })
      },
    })

    // wx.request({
    //   url: Config.url + '/api/consigneeInfo/find_consigneeInfo',
    //   method: 'POST',
    //   header: {
    //     'content-type': 'application/json',
    //     'accessToken': sessionId
    //   },
    //   data: {
    //     id: options.dingdanid || ''
    //   },
    //   success(res) {
    //     console.log(res);
    //     if (res.data.data.consigneeInfos == "") {
    //       that.setData({
    //         noData: false
    //       })
    //     } else {
    //       that.setData({
    //         addList: res.data.data.consigneeInfos,
    //       })
    //     }

    //     var arrays = res.data.data.consigneeInfos
    //     arrays.forEach(function(value, index, array) {

    //       if (value.isDefault == 1) {
    //         that.setData({
    //           indexAdree: index,

    //         })
    //       }

    //       　
    //     });
    //     console.log(that.data.addList)
    //   }
    // })
  },
  toorderDetails: function(e) {
    console.log(e);
    if (this.data.type == 1 || this.data.type==false) {
      return
    } else if(this.data.zf==9) {
      wx.redirectTo({
        // url: '/pages/orderDetails/orderDetails?id=' + e.currentTarget.dataset.id + "&addid=" + e.currentTarget.dataset.id + "&orderid=" + this.data.orderid + "&orderNum=" + e.currentTarget.dataset.ordernum + '&bookId=' + this.data.bookId + '&number=' + this.data.amount,
        url: '/pages/orderDetails/orderDetails?id=' + e.currentTarget.dataset.id + "&addid=" + e.currentTarget.dataset.id + "&orderid=" + this.data.orderid + "&orderNum=" + e.currentTarget.dataset.ordernum,
      })
    }else{
      return
    }
  },

  selAdree: function(e) {
    console.log(e.currentTarget.dataset.index)
    var sessionId = wx.getStorageSync('sessionId')
    var that = this
    var valindex = e.currentTarget.dataset.index
    var valId = e.currentTarget.id
    console.log(valId)
    this.setData({
      indexAdree: valindex,
    })

    wx.request({
        url: Config.url + '/api/consigneeInfo/edit_isDefault_address',
        method: 'POST',
        header: {
          'content-type': 'application/json',
          'accessToken': sessionId
        },
        data: {
          id: valId
        },
        success(res) {
          console.log(res)
        }
      })
      // wx.navigateTo({
      //   url: '../order/order',
      // })

  },
  goDetails: function() {
    wx.navigateTo({
      url: '../address/address'
    })
  },
  goEdit: function(e) {
    wx.navigateTo({
      url: '../editAddress/editAddress?id=' + e.currentTarget.id
    })
  },
  delAdress: function(e) {
    var sessionId = wx.getStorageSync('sessionId')
    var that = this
    var adressId = e.currentTarget.id
    wx.showModal({
      title: '提示',
      content: '是否删除',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: Config.url + '/api/consigneeInfo/delete_consigneeInfo',
            method: 'POST',
            header: {
              'content-type': 'application/json',
              'accessToken': sessionId
            },
            data: {
              id: adressId
            },
            success(res) {
              console.log(res)
              wx.request({
                url: Config.url + '/api/consigneeInfo/find_consigneeInfo',
                method: 'POST',
                header: {
                  'content-type': 'application/json',
                  'accessToken': wx.getStorageSync('sessionId')
                },
                data:{
                  id:""
                },
                success(res) {
                  console.log(res.data.data)
                  that.setData({
                    addList: res.data.data.consigneeInfos,
                  })
                  var arrays = res.data.data.consigneeInfos
                  arrays.forEach(function(value, index, array) {
                    if (value.isDefault == 1) {
                      that.setData({
                        indexAdree: index,
                      })
                    } else if (value.isDefault == 2){
                      that.setData({
                        indexAdree: false,
                      })
                    }
                  });
                  console.log(that.data.addList);
                  if (that.data.addList == '') {
                    that.setData({
                      noData: false,
                    })
                  }
                }
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })


  },
  radioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this
    wx.request({
      url: Config.url + '/api/consigneeInfo/find_consigneeInfo',
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'accessToken': wx.getStorageSync('sessionId')
      },
      data: {
        id: that.data.id || ''
      },
      success(res) {
        console.log(res);
        if (res.data.data.consigneeInfos == "") {
          that.setData({
            noData: false
          })
        } else {
          that.setData({
            addList: res.data.data.consigneeInfos,
          })
        }

        var arrays = res.data.data.consigneeInfos
        arrays.forEach(function (value, index, array) {

          if (value.isDefault == 1) {
            that.setData({
              indexAdree: index,

            })
          }


        });
        console.log(that.data.addList)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})